<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用另一个字符串填充字符串为指定长度</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.str-ireplace.html">str_ireplace</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.str-repeat.html">str_repeat</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.str-pad" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">str_pad</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">str_pad</span> &mdash; <span class="dc-title">使用另一个字符串填充字符串为指定长度</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.str-pad-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>str_pad</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$input</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$pad_length</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$pad_string</code><span class="initializer"> = &quot; &quot;</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$pad_type</code><span class="initializer"> = STR_PAD_RIGHT</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   该函数返回 <em><code class="parameter">input</code></em> 被从左端、右端或者同时两端被填充到制定长度后的结果。如果可选的 <em><code class="parameter">pad_string</code></em> 参数没有被指定，<em><code class="parameter">input</code></em> 将被空格字符填充，否则它将被 <em><code class="parameter">pad_string</code></em> 填充到指定长度。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-pad-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">input</code></em></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">pad_length</code></em></dt>

     <dd>

      <p class="para">
       如果 <em><code class="parameter">pad_length</code></em> 的值是负数，小于或者等于输入字符串的长度，不会发生任何填充。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">pad_string</code></em></dt>

     <dd>

      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        如果填充字符的长度不能被 <em><code class="parameter">pad_string</code></em> 整除，那么 <em><code class="parameter">pad_string</code></em> 可能会被缩短。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<em><code class="parameter">pad_type</code></em></dt>

     <dd>

      <p class="para">
       可选的 <em><code class="parameter">pad_type</code></em> 参数的可能值为 <strong><code>STR_PAD_RIGHT</code></strong>，<strong><code>STR_PAD_LEFT</code></strong> 或 <strong><code>STR_PAD_BOTH</code></strong>。如果没有指定 <em><code class="parameter">pad_type</code></em>，则假定它是 <strong><code>STR_PAD_RIGHT</code></strong>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-pad-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回填充后的字符串。
  </p>
 </div>



 <div class="refsect1 examples" id="refsect1-function.str-pad-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4778">
    <p><strong>Example #1 <span class="function"><strong>str_pad()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Alien"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"Alien&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"-="</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STR_PAD_LEFT</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"-=-=-Alien"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"_"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STR_PAD_BOTH</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"__Alien___"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">6&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"___"</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;"Alien_"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.str-ireplace.html">str_ireplace</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.str-repeat.html">str_repeat</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
